<!--
  // Creation History
  // --------------------------------------------------------------
  //	Company:			HTHK
  //	NAME:				TimeSheetMaintenance.jsp
  //	TITLE:				TimeSheet Maintenance
  //	FUNCTION:			input TimeSheet.
  //	BY:					Jet Tao
  //	DATE:				11/12/2003
  // UPDATE BY:			Beyes Xia
  //	UPDATE DATE:			2004-1-9
  //	LAST UPDATE DATE:		2004-2-9 8:54
  // --------------------------------------------------------------
-->
<%@ include file="../tssheader.jsp" %>
<%@ page import="tss.timesheet.*" %>
<%@ page import="tss.options.*" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %>
<%
try
{
	String begin_date = (String)session.getAttribute("weekly_begin_from");
	String end_date = (String)session.getAttribute("weekly_end_from");

	ProjectOptions ProjectOption=new ProjectOptions();
	JobOptions JobOption=new JobOptions();

	ChangeNum chgNum=new ChangeNum();

	Vector time_sheet=(Vector)request.getAttribute("TimeSheet");
	String staff_no=(String)request.getAttribute("StaffNo");
//****** Add By Beyes Xia
     String staff_name=(String)request.getAttribute("StaffName");
//******
	String position=(String)request.getAttribute("Grade");
	String start_date=(String)request.getAttribute("StartDate");
	String before_date=(String)request.getAttribute("before_date");
	String last_date=(String)request.getAttribute("last_date");
	String last_flag=(String)request.getAttribute("last_flag");
	String next_flag=(String)request.getAttribute("next_flag");
	String[] a_week=(String[])request.getAttribute("AWeek");
	int t_s_size=0;
	if(time_sheet!=null)
		t_s_size=time_sheet.size();
	float[] total_hours=new float[7];
	for(int i=0;i<total_hours.length;i++)
	{
		total_hours[i]=0;
	}
    SimpleDateFormat sdf1 = new SimpleDateFormat("dd MMM yyyy",Locale.US);
    SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd",Locale.US);
	for(int i=0;i<a_week.length;i++)
	{
		a_week[i]=sdf1.format(sdf2.parse(a_week[i]));
	}
%>
<html>
<script language="JavaScript1.1">
function autoFill(object)
{
	var num=object.value;
  	object.value=getRightNum(num,2,"Hours ");  
	if(object.value=="")
	{
		setTotal();
		return;
	}
	else
	{
		if((parseFloat(object.value)>24)||(parseFloat(object.value)<=0))
		{
			alert("The Hours must small than 24, and large than 0.");
			object.value="";
			setTotal();
			return;
		}
		setTotal();
	}
}
function setTotal()
{
	sun_total=0;
	mon_total=0;
	tue_total=0;
	wed_total=0;
	thu_total=0;
	fri_total=0;
	sat_total=0;
	for(i=0;i<5+<%=t_s_size%>;i++)
	{
		if(fm.enable_flag[i].value=="1")
		{
			sun_total+=(fm.sun[i].value=="")?0:parseFloat(fm.sun[i].value);
			mon_total+=(fm.mon[i].value=="")?0:parseFloat(fm.mon[i].value);
			tue_total+=(fm.tue[i].value=="")?0:parseFloat(fm.tue[i].value);
			wed_total+=(fm.wed[i].value=="")?0:parseFloat(fm.wed[i].value);
			thu_total+=(fm.thu[i].value=="")?0:parseFloat(fm.thu[i].value);
			fri_total+=(fm.fri[i].value=="")?0:parseFloat(fm.fri[i].value);
			sat_total+=(fm.sat[i].value=="")?0:parseFloat(fm.sat[i].value);
		}
	}
	
	fm.sun_total.value=sun_total;
	fm.mon_total.value=mon_total;
	fm.tue_total.value=tue_total;
	fm.wed_total.value=wed_total;
	fm.thu_total.value=thu_total;
	fm.fri_total.value=fri_total;
	fm.sat_total.value=sat_total;
	
	fm.sun_total.value=getRightNum(fm.sun_total.value,2,"Hours ");
	fm.mon_total.value=getRightNum(fm.mon_total.value,2,"Hours ");
	fm.tue_total.value=getRightNum(fm.tue_total.value,2,"Hours ");
	fm.wed_total.value=getRightNum(fm.wed_total.value,2,"Hours ");
	fm.thu_total.value=getRightNum(fm.thu_total.value,2,"Hours ");
	fm.fri_total.value=getRightNum(fm.fri_total.value,2,"Hours ");
	fm.sat_total.value=getRightNum(fm.sat_total.value,2,"Hours ");

}
function DoOnChange()
{
	for(i=0;i<5+<%=t_s_size%>;i++)
	{
		if((fm.job[i].value=="") || (fm.project[i].value==""))
		{
			fm.enable_flag[i].value="0";
			fm.sun[i].disabled=true;
			fm.mon[i].disabled=true;
			fm.tue[i].disabled=true;
			fm.wed[i].disabled=true;
			fm.thu[i].disabled=true;
			fm.fri[i].disabled=true;
			fm.sat[i].disabled=true;
		}
		else
		{
			fm.enable_flag[i].value="1";
			fm.sun[i].disabled=false;
			fm.mon[i].disabled=false;
			fm.tue[i].disabled=false;
			fm.wed[i].disabled=false;
			fm.thu[i].disabled=false;
			fm.fri[i].disabled=false;
			fm.sat[i].disabled=false;
		}
	}
	setTotal();
}
function init()
{
   var today = new Date();
   month=(today.getMonth()+1>9)?(today.getMonth()+1):"0"+(today.getMonth()+1);
   date=(today.getDate()>9)?today.getDate():("0"+today.getDate());
   fm.now_date.value=today.getYear()+"-"+month+"-"+date;
	
	for(i=0;i<5+<%=t_s_size%>;i++)
	{
		if(fm.enable_flag[i].value=="0")
		{
			fm.sun[i].disabled=true;
			fm.mon[i].disabled=true;
			fm.tue[i].disabled=true;
			fm.wed[i].disabled=true;
			fm.thu[i].disabled=true;
			fm.fri[i].disabled=true;
			fm.sat[i].disabled=true;
		}
	}
}
function formValidation()
{
	kickNull();
	if(!check_total())
		return false;
	fm.operation_type.value="save";
	return true;
}
function check_total()//the total must equal to 0 or 8.
{
	if(fm.sun_total.value != 0)
	{
		alert("Total hours of Sunday must equal to 0.");
		return false;
	}
	if(fm.mon_total.value != 0 && fm.mon_total.value != 8)
	{
		alert("Total hours of Monday must equal to 0 or 8.");
		return false;
	}
	if(fm.tue_total.value != 0 && fm.tue_total.value != 8)
	{
		alert("Total hours of Tuesday must equal to 0 or 8.");
		return false;
	}
	if(fm.wed_total.value != 0 && fm.wed_total.value != 8)
	{
		alert("Total hours of Wednesday must equal to 0 or 8.");
		return false;
	}
	if(fm.thu_total.value != 0 && fm.thu_total.value != 8)
	{
		alert("Total hours of Thursday must equal to 0 or 8.");
		return false;
	}
	if(fm.fri_total.value != 0 && fm.fri_total.value != 8)
	{
		alert("Total hours of Friday must equal to 0 or 8.");
		return false;
	}
	if(fm.sat_total.value != 0)
	{
		alert("Total hours of Saturday must equal to 0.");
		return false;
	}
	return true;
}
function kickNull()//kill those do not have data(it's "hours") from "sun" to "sat".
{
	total_record=0;
	for(i=0;i<5+<%=t_s_size%>;i++)
	{
		if(fm.enable_flag[i].value=="1")
		{
		total_record++;			
		if((fm.sun[i].value=="")&&(fm.mon[i].value=="")&&(fm.tue[i].value=="")&&(fm.wed[i].value=="")&&(fm.thu[i].value=="")&&(fm.fri[i].value=="")&&(fm.sat[i].value==""))
			{
				total_record--;
				fm.job[i].value="";
				fm.project[i].value="";
				fm.enable_flag[i].value="0";
				fm.sun[i].disabled=true;
				fm.mon[i].disabled=true;
				fm.tue[i].disabled=true;
				fm.wed[i].disabled=true;
				fm.thu[i].disabled=true;
				fm.fri[i].disabled=true;
				fm.sat[i].disabled=true;
			}
		}
	}
	fm.total_record.value=total_record;
}

function back()
{
	fm1.submit();
}

</script>

<body onload='init()'>
<h2>Time Sheet Maintenance</h2>
<form id=fm method=post action='<%=contextPath%>/servlet/tss.weeklytimesheet.WeeklyTimeSheetInput' >

<input type=hidden name='selected_date' value=<%=start_date%>>
<input type=hidden name='now_date' value=''>
<input type=hidden name='which_week'>
<input type=hidden name='operation_type' value="edit">
<input type=hidden name='total_record' value="0">
<input type=hidden name='staff_no' value='<%=staff_no%>'>
<input type=hidden name='position' value='<%=position%>'>

<table>
<tr>
<td>Start Date</td>
<td><input type=text value='<%=start_date%>' class=read readonly ></td>
</tr>
</table>
<p>
<table cols=2 border=1 >
<thead>
<td nowrap style='WIDTH:15%'><b><br><br>Project</b></td>
<td nowrap style='WIDTH:15%'><b><br><br>Job</b></td>
<!-- Add By Beyes Xia -->
<!--	2004-4-20 10:32
<td nowrap style='WIDTH:10%'><b><br><br>Over Time</b></td>
-->
<!-- ---------------- -->
<td nowrap style='WIDTH:9%'><b>Sun<br><%=a_week[0].substring(3,6)%><br><%=a_week[0].substring(0,2)%></b></td>
<td nowrap style='WIDTH:9%'><b>Mon<br><%=a_week[1].substring(3,6)%><br><%=a_week[1].substring(0,2)%></b></td>
<td nowrap style='WIDTH:9%'><b>Tue<br><%=a_week[2].substring(3,6)%><br><%=a_week[2].substring(0,2)%></b></td>
<td nowrap style='WIDTH:9%'><b>Wed<br><%=a_week[3].substring(3,6)%><br><%=a_week[3].substring(0,2)%></b></td>
<td nowrap style='WIDTH:9%'><b>Thu<br><%=a_week[4].substring(3,6)%><br><%=a_week[4].substring(0,2)%></b></td>
<td nowrap style='WIDTH:9%'><b>Fri<br><%=a_week[5].substring(3,6)%><br><%=a_week[5].substring(0,2)%></b></td>
<td nowrap style='WIDTH:9%'><b>Sat<br><%=a_week[6].substring(3,6)%><br><%=a_week[6].substring(0,2)%></b></td>
</thead>
<%
	if(time_sheet!=null)
	{
		int size=time_sheet.size();
		for(int i=0;i<size;i++)
		{
			String[] time_sheet_str=(String[])time_sheet.get(i);
			total_hours[0]+=(time_sheet_str[2].equals("")?0:Float.parseFloat(time_sheet_str[2]));
			total_hours[1]+=(time_sheet_str[3].equals("")?0:Float.parseFloat(time_sheet_str[3]));
			total_hours[2]+=(time_sheet_str[4].equals("")?0:Float.parseFloat(time_sheet_str[4]));
			total_hours[3]+=(time_sheet_str[5].equals("")?0:Float.parseFloat(time_sheet_str[5]));
			total_hours[4]+=(time_sheet_str[6].equals("")?0:Float.parseFloat(time_sheet_str[6]));
			total_hours[5]+=(time_sheet_str[7].equals("")?0:Float.parseFloat(time_sheet_str[7]));
			total_hours[6]+=(time_sheet_str[8].equals("")?0:Float.parseFloat(time_sheet_str[8]));
%>
<tr>
  <td>
    <select name=project onchange='DoOnChange()'>
	<%=ProjectOption.getDefualtOptions(staff_no,time_sheet_str[0])%>
    </select>
  </td>
  <td>
    <select name=job onchange='DoOnChange()'>
       <%=JobOption.getOptions(time_sheet_str[1])%>
	</select>
  </td>
<!-- Add By Beyes Xia -->
<!--
<td align=center><input type=checkbox name='overtime' class=btn <%=time_sheet_str[9].equals("Y")?"checked":""%> disabled></td>
<input type=hidden name='otvalue' value='<%=time_sheet_str[9]%>'>
-->
<!-- ---------------- -->
<td><INPUT type=text name='sun' value='<%=chgNum.getFixNum(time_sheet_str[2],2)%>'  onBlur="autoFill(this);"     maxlength=5 size=6></td>
<td><INPUT type=text name='mon' value='<%=chgNum.getFixNum(time_sheet_str[3],2)%>'  onBlur="autoFill(this);"     maxlength=5 size=6></td>
<td><INPUT type=text name='tue' value='<%=chgNum.getFixNum(time_sheet_str[4],2)%>'  onBlur="autoFill(this);"     maxlength=5 size=6></td>
<td><INPUT type=text name='wed' value='<%=chgNum.getFixNum(time_sheet_str[5],2)%>'  onBlur="autoFill(this);"     maxlength=5 size=6></td>
<td><INPUT type=text name='thu' value='<%=chgNum.getFixNum(time_sheet_str[6],2)%>'  onBlur="autoFill(this);"     maxlength=5 size=6></td>
<td><INPUT type=text name='fri' value='<%=chgNum.getFixNum(time_sheet_str[7],2)%>'  onBlur="autoFill(this);"     maxlength=5 size=6></td>
<td><INPUT type=text name='sat' value='<%=chgNum.getFixNum(time_sheet_str[8],2)%>'  onBlur="autoFill(this);"     maxlength=5 size=6></td>
</tr>
<input type=hidden name='enable_flag' value='1'>

<%
		}
	}
%>
<%
	for(int i=0;i<5;i++)
	{
%>
<tr>
  <td>
    <select name=project  onchange='DoOnChange()'>
	<%=ProjectOption.getDefualtOptions(staff_no,"")%>
	</select>
  </td>
  <td>
    <select name=job  onchange='DoOnChange()'>
       <%=JobOption.getOptions("")%>
	</select>
  </td>
<!-- Add By Beyes Xia -->
<!--
<td  align=center><input type=checkbox name='overtime' class=btn ></td>
<input type=hidden name='otvalue' value='N'>
-->
<!-- ---------------- -->
<td><INPUT type=text name='sun' value=''  onBlur="autoFill(this);"    maxlength=5 size=6></td>
<td><INPUT type=text name='mon' value=''  onBlur="autoFill(this);"    maxlength=5 size=6></td>
<td><INPUT type=text name='tue' value=''  onBlur="autoFill(this);"    maxlength=5 size=6></td>
<td><INPUT type=text name='wed' value=''  onBlur="autoFill(this);"    maxlength=5 size=6></td>
<td><INPUT type=text name='thu' value=''  onBlur="autoFill(this);"    maxlength=5 size=6></td>
<td><INPUT type=text name='fri' value=''  onBlur="autoFill(this);"    maxlength=5 size=6></td>
<td><INPUT type=text name='sat' value=''  onBlur="autoFill(this);"    maxlength=5 size=6></td>
</tr>
<input type=hidden name='enable_flag' value='0'>

<%
	}
%>
<tr>
<td colspan=2>Total(hours)</td>
<td><INPUT type=text name='sun_total' value='<%=chgNum.getFixNum(String.valueOf(total_hours[0]),2)%>'  maxlength=5 size=6 class=read readonly></td>
<td><INPUT type=text name='mon_total' value='<%=chgNum.getFixNum(String.valueOf(total_hours[1]),2)%>'  maxlength=5 size=6 class=read readonly></td>
<td><INPUT type=text name='tue_total' value='<%=chgNum.getFixNum(String.valueOf(total_hours[2]),2)%>'  maxlength=5 size=6 class=read readonly></td>
<td><INPUT type=text name='wed_total' value='<%=chgNum.getFixNum(String.valueOf(total_hours[3]),2)%>'  maxlength=5 size=6 class=read readonly></td>
<td><INPUT type=text name='thu_total' value='<%=chgNum.getFixNum(String.valueOf(total_hours[4]),2)%>'  maxlength=5 size=6 class=read readonly></td>
<td><INPUT type=text name='fri_total' value='<%=chgNum.getFixNum(String.valueOf(total_hours[5]),2)%>'  maxlength=5 size=6 class=read readonly></td>
<td><INPUT type=text name='sat_total' value='<%=chgNum.getFixNum(String.valueOf(total_hours[6]),2)%>'  maxlength=5 size=6 class=read readonly></td>
</tr>

</table>
</form>

<form id=fm1 method=post action='<%=contextPath%>/servlet/tss.weeklytimesheet.WeeklyTSSearch' >
<input type=hidden name='date_from' value='<%= begin_date %>'>
<input type=hidden name='date_to' value='<%= end_date %>'>
<input type=hidden name='staff_no' value='<%= user.getStaff_id()%>'>
<input type=hidden name='user_name' value='<%= user.getUser_name()%>'>
</form>

<br>
<hr>
<p>
	<INPUT type=button value='Save' onclick='save()' >
	<INPUT type=button value='Back' onclick='back()' >
<%@ include file="../tssfooter.jsp" %>
</body>
</html>
<%
	}
	catch(Exception e)
	{
		e.printStackTrace();
	}
	
%>
